﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Enum;
using DFGK_WMS.Model.Models;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NetTaste;
using System.ComponentModel;
using System.Data;
using System.Reflection;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.基础数据
{
    /// <summary>
    /// 编码规则管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BarCodeRuleController : ControllerBase
    {
        private readonly ILogRepository logRepository;
        public IBarCodeRuleRepository BarCodeRuleService { get; set; }
        public BarCodeRuleController(IBarCodeRuleRepository barcoderuleservice, ILogRepository logRepository)
        {
            BarCodeRuleService = barcoderuleservice;
            this.logRepository = logRepository;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddCodeRule(BarCodeRuleDto dto)
        {          
            bool rule = BarCodeRuleService.AddCodeRule(dto);
            if (rule)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 4;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "创建了编码规则" + dto.Name;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "添加成功", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败,该规则类型已存在！", "500"));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateCodeRule(BarCodeRuleDto dto)
        {
            if(dto.Id<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(dto.Code))
            {
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            }
            bool rule = BarCodeRuleService.UpdateCodeRule(dto);
            if (rule)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 6;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "修改了编码规则" + dto.Name;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "修改成功", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败", "500"));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DelCodeRule(int ids)
        {
            if (ids <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool rule = BarCodeRuleService.DelCodeRule(ids);
            if (rule)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int rid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 5;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = rid;
                logg.Description = uname + "删除了编码规则";
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "删除成功", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败", "500"));
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetCodeRules(string? name, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount = 0;
            var list = BarCodeRuleService.GetCodeRules(name, pageIndex, pageSize, out totalCount);
            return Ok(BaseResult.Result(list, "查询成功", "200", totalCount));
        }

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetCodeRule(int ids)
        {
            if (ids <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var rule = BarCodeRuleService.GetCodeRule(ids);
            return Ok(BaseResult.Result(rule, "查询成功", "200"));
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetCodeRulest()
        {
            var rule = BarCodeRuleService.GetCodeRulest();
            return Ok(BaseResult.Result(rule, "查询成功", "200"));
        }

        /// <summary>
        /// 获取编码规则
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetBarCodeSample(int type)
        {
            if (type <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            string result= BarCodeRuleService.GetBarCodeSample(type);
            if(!string.IsNullOrEmpty(result))
            {
                return Ok(BaseResult.Result(result, "获取成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "无规则或规则已禁用！", "501"));
        }

        /// <summary>
        /// 枚举编码规则
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetEnumCodeRulesType()
        {
            var list = BaseEnum.GetCodeRulesList<CodeRulesType>();

            return Ok(list);
        }
    }
}
